package com.study.api;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.Lists;
import com.study.api.ApiController.DataDTO.Stu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.net.InetAddress;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * @author zhuzi
 * 2024/4/2 11:32
 */
@Api(tags = "测试IP")
@Slf4j
@RestController
@RequestMapping("api")
public class ApiController {

    @Data
    @Accessors(chain = true)
    public static class StuObj {
        //byte[] largeArray = new byte[1024 * 1024 * 5]; // 5MB
    }

    static ArrayList<StuObj> objLists = Lists.<StuObj>newArrayList();

    @ApiOperation("本机IP")
    @GetMapping("ip")
    public String localIp() {

        for (int i = 0; i < 20000; i++) {
            objLists.add(new StuObj());
        }

        String localhostStr = NetUtil.getLocalhostStr();
        System.out.println(localhostStr);

        InetAddress localhost = NetUtil.getLocalhost();
        return localhostStr;
    }

    @ApiOperation("测试接口formPost")
    @PostMapping("formPost")
    public String formPost(Stu stu) {
        //log.info("接收到数据{}", stu);
        return LocalDateTime.now().toString();
    }

    @GetMapping("c")
    public Stu formPosts() {
        Stu stus = new Stu();
        stus.setId(IdUtil.getSnowflakeNextId()).setAge(29).setName("透明");
        return stus;
    }

    @ApiImplicitParam(name = "name", value = "姓名", required = true)
    @ApiOperation("测试接口RequestBody")
    @PostMapping("/body")
    public ResponseEntity body(@RequestBody String fileResp) {
        return ResponseEntity.ok(fileResp);
    }

    @ApiOperation(value = "测试")
    @ApiOperationSupport(order = 1)
    @GetMapping("name")
    public String name(String name) {

        return StrUtil.repeat(name, 2);
    }

    public interface DataDTO {

        @Data
        @Accessors(chain = true)
        class Stu {
            private String name;
            private Long id;
            private Integer age;
        }
    }


    @GetMapping("/rate-limited")
    //@RateLimiter(name = "processService", fallbackMethod = "processFallback")
    //@RateLimiter(key = "#request", capacity = 10, refreshPeriod = 1)
    public Mono<String> rateLimited() {
        return Mono.just("Hello, World!");
    }

}
